let nodemailer = require("nodemailer");

// 引入数据库
const {
  mysqlFun
} = require('../mysql/mysql');
// 引入加密
const md5 = require("md5")
let transporter = nodemailer.createTransport({ //邮件传输
  host: "smtp.163.com", //网易云 smtp服务器地址
  secureConnection: false, //是否使用安全连接，对https协议的
  port: 465, //服务所占用的端口
  auth: {
    user: "maygodblessyouqi@163.com", //开启SMTP的邮箱，有用发送邮件
    pass: "YURFSSNYKPTOBSBG" //授权码
  }
});

exports.postCode = function (app) {
  let code = "";
  app.post("/QQEmail", (req, res) => {
    // 用户传过来的邮箱地址
    // 用js判断邮箱是否合法
    // 生成一个code码
    while (code.length < 5) {
      code += Math.floor(Math.random() * 10);
    }
    let mailOption = {
      from: "maygodblessyouqi@163.com",
      to: req.body.QQEmail, //收件人
      subject: "您正在注册云幻想", //纯文本
      html: "<h1>您的验证码为<span style='color:red'>" + code + "</span>请不要把验证码泄露给其他人。如非本人操作，可不用理会！</h1>"
    };

    transporter.sendMail(mailOption, function (error, info) {
      if (error) {
        res.send("1");
        return console.info(error);
      } else {
        req.session.yanzhengma = code;
        res.send("2");
        console.info("Message send" + code);
      }
    })

    res.json({
      code: 'B0001',
      msg: '验证码发送成功'
    })
  })

  let forgetcode = "";
  app.post("/setforget", (req, res) => {
    // 用户传过来的邮箱地址
    // 用js判断邮箱是否合法
    // 生成一个code码
    while (forgetcode.length < 5) {
      forgetcode += Math.floor(Math.random() * 10);
    }
    const sql = `select * from userdata where email = '${req.body.emailInput}'`;

    function obj(data) {
      if (data === undefined) {
        return;
      }
      // 通过长度判断是否有返回数据
      if (data.length === 0) {
        res.json({
          code: 'A0000'
        })
        return;
      } else {
        let mailOption = {
          from: "maygodblessyouqi@163.com",
          to: req.body.emailInput, //收件人
          subject: "您正在找回密码", //纯文本
          html: "<h1>您的验证码为<span style='color:red'>" + forgetcode + "</span>请不要把验证码泄露给其他人。如非本人操作，可不用理会！</h1>"
        };
        transporter.sendMail(mailOption, function (error, info) {
          if (error) {
            res.send("1");
            return console.info(error);
          } else {
            req.session.yanzhengma = forgetcode;
            res.send("2");
            console.info("Message send" + forgetcode);
          }
        })
        res.json({
          code: 'B0001'
        })
      }
    }
    mysqlFun(sql, obj);
  })

  // 注册账号验证码的验证
  app.post('/setEmail', (req, res) => {
    // 验证用户输入的验证码是否正确
    if (code === req.body.isCode) {
      res.json({
        code: 'B0002',
        msg: '验证码正确'
      })
      return;
    }
    res.json({
      code: 'B0003',
      msg: '验证码输入错误'
    })
  })

  // 找回密码验证码的判断
  app.post('/ForgetCode', (req, res) => {
    // 验证用户输入的验证码是否正确
    if (forgetcode === req.body.isCode) {
      res.json({
        code: 'C0001'
      })
      return;
    }
    res.json({
      code: 'B0004'
    })
  })

  // 判断邮箱中是否存在账号
  app.post('/queryUser', (req, res) => {
    let sql = `select * from userdata where user='${req.body.userInput}'`

    function obj(data) {
      if (data === undefined) {
        return;
      }
      if (data.length === 0) {
        // 没有账号
        res.json({
          code: 'Q0001'
        })
        return;
      }
      // 账号已存在
      res.json({
        code: 'Q0002'
      })
    }
    mysqlFun(sql, obj);
  })

  // 更改账号和密码
  app.post('/ResetPassword', (req, res) => {
    let password = md5(req.body.passwordInput)
    let sql = `UPDATE userdata SET user='${req.body.userInput}',password='${password}' WHERE email='${req.body.emailInput}'`

    function obj(data) {
      if (data === undefined) {
        return;
      }
      // 密码重置成功
      res.json({
        code: 'R0001'
      })
    }
    mysqlFun(sql, obj);
  })
}